(function(){

    var Cheltuiala = Models.CheltuialaBase.extend(
    {
        // list of some form field names
        _fields: {
            coloana:    'coloana_id',
            data:       'data_emiterii',
            scadenta:   'scadenta',
            target:     'target_id',
            tipCheltuiala: 'tip_cheltuiala_id'
        },

        // campul 'scadenta' va fi afisat cand sunt selectate tipurile de cheltuieli listate mai jos
        _showScadenta: [1], // 1 = factura

        _selectors: {
            translationTable: 'input.translation:first'
        },

        _initColumnSelection: function()
        {
            var self = this;

            // elementul de unde se selecteaza coloana pe care se distribuie o cheltuiala
            var $coloanaField   = this.getField( this._fields.coloana ),
                $translation    = this.getElement().find( this._selectors.translationTable ),
                translation     = $.parseJSON( $translation.val() );

            // cand se selecteaza o coloana, se afiseaza, dupa caz, si alte campuri suplimentare in functie de criteriul asociat coloanei
            $coloanaField.change( function(){
                var selectedItem = $(this).val();
                self.Criterii.showForm( translation[selectedItem] );
            })
        },

        _initDateFields: function()
        {
            var $dataEmiterii   = this.getField( this._fields.data ),
                $scadenta       = this.getField( this._fields.scadenta );

            $dataEmiterii.add($scadenta).datepicker().attr('readonly', 'readonly');

            $dataEmiterii.datepicker('option', {
                maxDate: new Date(),
                onSelect: function(dateText, inst) {
                    $scadenta.datepicker('option', 'minDate', dateText);
                }
            })

            $scadenta.datepicker('option', {
                minDate: $dataEmiterii.datepicker('getDate')
            })

        },

        /**
        * Scadenta este afisata doar cand este inregistrata o factura
        */
        _initScadenta: function()
        {
            var self = this,
                $scadentaRow    = this.getField(this._fields.scadenta).parents('tr.field-row:first'),
                $tipCheltuiala  = this.getField(this._fields.tipCheltuiala),
                $scadentaInfo   = this.getElement().find('.scadenta-info');

            var check = function(tipCheltuiala){
                var $elements = $scadentaRow.add($scadentaInfo);
                self._showScadenta.search(tipCheltuiala) !== false ?
                    $elements.show() :
                    $elements.hide();
            }

            $tipCheltuiala.change(function(){
                check($(this).val());
            })

            check($tipCheltuiala.val());
        }

    });

    ////////////////////////////////////////////////////////////////////////////
    // attach the defined models to Models global container ////////////////////
    ////////////////////////////////////////////////////////////////////////////

    Models.Cheltuieli = {
        Add: Cheltuiala
    }

})()


